package munyorderweb;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import munyorderweb_DB.Change_Bean;

public class Menu_conf extends HttpServlet {

	String sql;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		order(req,resp);
	}

	protected void order(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");

		ArrayList<Change_Bean> list = new ArrayList<Change_Bean>();
		Change_Bean change = null;

		int i;

		list = new ArrayList<Change_Bean>();
		String[] M_no = req.getParameterValues("no");
		String[] M_name = req.getParameterValues("name");
		String[] M_price = req.getParameterValues("price");
		String[] C_no = req.getParameterValues("C_no");
		if(M_no != null){
			for(i = 0; i<M_no.length; i++){
				System.out.println(M_no[i]);
				System.out.println(M_name[i]);
				System.out.println(M_price[i]);
				System.out.println(C_no[i]);
				change = new Change_Bean();
				if(M_no[i] != "" && M_name[i] != "" && M_price[i] != "" && C_no[i] != ""){
					change.setMenu_no(Integer.parseInt(M_no[i]));
					change.setMenu_name(M_name[i]);
					change.setMenu_price(Integer.parseInt(M_price[i]));
					change.setCategory_no(Integer.parseInt(C_no[i]));
				}else{
					change.setMenu_no(Integer.parseInt(M_no[i]));
					change.setMenu_name("");
					change.setMenu_price(0);
					change.setCategory_no(0);
				}
				list.add(change);
			}
		}
		if(req.getParameterValues("newName") != null && req.getParameterValues("newPrice") != null
				&& req.getParameterValues("newNo") != null){
			String[] newName = req.getParameterValues("newName");
			String[] newPrice = req.getParameterValues("newPrice");
			String[] newNo = req.getParameterValues("newNo");
			for(int j=0; j<newName.length; j++){
				change = new Change_Bean();
				System.out.println(newName[j]);
				System.out.println(newPrice[j]);
				System.out.println(newNo[j]);
				if(newName[j] != "" && newPrice[j] != "" && newNo[j] != ""){
					change.setMenu_no(0);
					change.setMenu_name(newName[j]);
					change.setMenu_price(Integer.parseInt(newPrice[j]));
					change.setCategory_no(Integer.parseInt(newNo[j]));
					list.add(change);
				}
			}
		}

		HttpSession session_M = req.getSession();
		session_M.setAttribute("Menu", list);

		//表示用Jspファイルを呼出して画面上に表示
		req.setAttribute("Menu_conf", list);
		RequestDispatcher dispatcher = req.getRequestDispatcher("Menu_conf.jsp");
		dispatcher.include(req, resp);
	}
}